package com.jintoufs.web.action.ticker;

import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.sys.Dictionary;
import com.jintoufs.service.baseinfo.SiteService;
import com.jintoufs.service.baseinfo.TickerService;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.web.action.support.SupportAction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * Created by xufei on 2017/10/31.
 */
@Controller
@Scope("prototype")
@RequestMapping("/ticker")
public class TickerAction extends SupportAction {

    @Resource
    private TickerService tickerService;
    @Resource
    private SiteService siteService;

    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping("/last")
    public String last(Model model, Integer siteAId, Integer siteBId,Integer min) throws IOException {
        String message = "";
        List<Map<String, Object>> trades = new ArrayList<>();
        Map<String, Object> siteMap = new HashMap<>();
        siteMap.put("status", 1);
        List<Map<String, Object>> sites = siteService.queryByParams(siteMap);
        //查询钱10分钟内的数据
        if(min==null){
            min=20;
        }
        String minTime = DateUtils.getDateBeforeMin(min);
        Map siteAmap = new HashMap();
       siteAmap.put("startTime", minTime);
        siteAmap.put("siteId", siteAId);
        siteAmap.put("withdrawStatus", Constants.SITE_COIN_STATUS_ENABLE);
        List<Map<String, Object>> listA = tickerService.getLastestPrice(siteAmap);
        Map siteBmap = new HashMap();
       siteBmap.put("startTime", minTime);
        siteBmap.put("siteId", siteBId);
        siteBmap.put("rechargeStatus", Constants.SITE_COIN_STATUS_ENABLE);
        List<Map<String, Object>> listB = tickerService.getLastestPrice(siteBmap);
        List<Dictionary> list2 = dictionaryService.getDicByKeyNo("trade_volume");
        double tradeVolume = 100000;
        if (list2 != null && list2.size() > 0) {
            tradeVolume = Double.parseDouble(list2.get(0).getKeyValue());
        }

        for (Map<String, Object> map : listA) {
            for (Map<String, Object> map2 : listB) {
                long id = Long.parseLong(map.get("id").toString());
                long id2 = Long.parseLong(map2.get("id").toString());
                String siteName = map.get("site_name").toString();
                String siteId1 = map.get("site_id").toString();
                String coinId1 = map.get("coin_id").toString();
                String siteName2 = map2.get("site_name").toString();
                if (id == id2 || siteName.equals(siteName2)) {
                    continue;
                }
                double last = Double.parseDouble(map.get("sell").toString());
                String coinName = map.get("coin_rename").toString();
                String uniteCode = map.get("unit_code").toString();
                String sell_trade_fee1 = map.get("sell_trade_fee").toString();
                String buy_trade_fee1 = map.get("buy_trade_fee").toString();
                String date = map.get("date").toString();
                String withdraw_status = map.get("withdraw_status").toString();
                double vol1 = Double.parseDouble(map.get("vol").toString());


                double last2 = Double.parseDouble(map2.get("buy").toString());
                String coinName2 = map2.get("coin_rename").toString();
                String uniteCode2 = map2.get("unit_code").toString();
                String siteId2 = map2.get("site_id").toString();
                String coinId2 = map2.get("coin_id").toString();
                String sell_trade_fee2 = map2.get("sell_trade_fee").toString();
                String buy_trade_fee2 = map2.get("buy_trade_fee").toString();
                String date2 = map2.get("date").toString();
                String recharge_status = map2.get("recharge_status").toString();
                double vol2 = Double.parseDouble(map2.get("vol").toString());


                //属于同一个币种，则进行比较
                // 转换为CNY后计算
                double price1 = 0, price2 = 0;
                if (coinName.equalsIgnoreCase(coinName2)&& recharge_status.equals(Constants.SITE_COIN_STATUS_ENABLE)&& withdraw_status.equals(Constants.SITE_COIN_STATUS_ENABLE)) {
                    price2 = getPrice(uniteCode2, listB);
                    price1 = getPrice(uniteCode, listA);
                    Map<String, Object> entity = new HashMap();
                    entity.put("siteName1", siteName);
                    entity.put("siteId1", siteId1);
                    entity.put("coinId1", coinId1);
                    entity.put("coinName1", coinName);
                    entity.put("unitCode1", uniteCode);
                    entity.put("sell_trade_fee1", sell_trade_fee1);
                    entity.put("buy_trade_fee1", buy_trade_fee1);
                    entity.put("date1", DateUtils.dateDiff('m',new Date(),DateUtils.str2Date(date,DateUtils.datetimeFormat)));
                    entity.put("last1", last);
                    entity.put("vol1", vol1*price1);
                    entity.put("cnyPrice1", last * price1);
                    entity.put("siteName2", siteName2);
                    entity.put("coinName2", coinName2);
                    entity.put("siteId2", siteId2);
                    entity.put("coinId2", coinId2);
                    entity.put("unitCode2", uniteCode2);
                    entity.put("sell_trade_fee2", sell_trade_fee2);
                    entity.put("buy_trade_fee2", buy_trade_fee2);
                    entity.put("date2", DateUtils.dateDiff('m',new Date(),DateUtils.str2Date(date2,DateUtils.datetimeFormat)));
                    entity.put("last2", last2);
                    entity.put("vol2", vol2*price2);
                    entity.put("cnyPrice2", last2 * price2);
                    double tradeVol1 = vol1 * price1;
                    double tradeVol2 = vol2 * price2;
                    entity.put("profit", (last2 * price2 - last * price1) / (last * price1) * 100);
                    double pure=(last2 * price2 - last * price1) / (last * price1) - Double.parseDouble(buy_trade_fee1) - Double.parseDouble(sell_trade_fee2);
                    entity.put("pure_profit", pure * 100);
                    if(tradeVol1>tradeVolume&&tradeVol2>tradeVolume){

                        if(siteAId!=null||siteBId!=null){
                            trades.add(entity);
                        }else{
                            if (pure>0) {
                                trades.add(entity);
                            }
                        }
                    }

                } else {
//                    Map<String, Object> entity = new HashMap();
//                    entity.put("siteName1", siteName);
//                    entity.put("coinName1", coinName);
//                    entity.put("unitCode1", uniteCode);
//                    entity.put("date1", DateUtils.getDateStringFromString(date,DateUtils.HHMM));
//                    entity.put("last1", last);
//                    entity.put("vol1",vol1);
//                    entity.put("cnyPrice1", last * price1);
//                    if (!trades.contains(entity)) {
//                        trades.add(entity);
//                    }
                }
            }
        }
        model.addAttribute("sites", sites);
        model.addAttribute("trades", trades);
        model.addAttribute("siteAId", siteAId);
        model.addAttribute("siteBId", siteBId);
        model.addAttribute("message", message);
        model.addAttribute("min", min);
        return "ticker/market";
    }


}
